<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\Admin\AnalysisRepository;
use App\Repositories\Admin\CategoryRepository;
use App\Repositories\Admin\CategoryStatisticsRepository;
use App\Repositories\Admin\StatisticsRepository;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class AnalysisController extends Controller
{
    protected $categoryRepository;
    protected $analysisRepository;
    protected $statisticsRepository;
    protected $categoryStatisticsRepository;
    public function __construct(AnalysisRepository $analysisRepository)
    {
        $categoryRepository = new CategoryRepository();
        $statisticsRepository = new StatisticsRepository();
        $categoryStatisticsRepository = new CategoryStatisticsRepository();
        $this->analysisRepository = $analysisRepository;
        $this->categoryRepository = $categoryRepository;
        $this->statisticsRepository = $statisticsRepository;
        $this->categoryStatisticsRepository = $categoryStatisticsRepository;
    }

//    public function addCategoriesPerdayNum(){
//        $id1 = 11;
//        Redis::set('dayPictureCategoryId_'.$id1, 2);
//        $id2 = 12;
//        Redis::set('dayPictureCategoryId_'.$id2, 3);
//
//        echo Redis::get('dayPictureCategoryId_'.$id1);
//        echo Redis::get('dayPictureCategoryId_'.$id2);
//    }
    /**
     * 获取所有信息
     */
    public function getAll(){
        $perdayUsers = $this->getDayUserNum(); //每日新增用户数
        $users = $this->getSexNum(); //用户总数和男女数
        $pictures = $this->getPictureNum(); //图片总数
        $perdayPictures = $this->getDayPictureNum(); //每日图片新增数
        $categoriesPerdayNum = $this->getCategoriesPerdayNum(); //每日各分组图片新增数

        return json_encode(['code'=>200,'perdayUsers'=>$perdayUsers,'users'=>$users,'pictures'=>$pictures,'perdayPictures'=>$perdayPictures,'categoriesPerdayNum'=>$categoriesPerdayNum]);
    }
    /**
     * 重置所有信息
     */
    public function refreshAll(){
        $perdayUsers = $this->getDayUserNum(); //每日新增用户数
        $users = $this->getSexNum(); //用户总数和男女数
        $pictures = $this->getPictureNum(); //图片总数
        $perdayPictures = $this->getDayPictureNum(); //每日图片新增数
        $categoriesPerdayNum = $this->getCategoriesPerdayNum(); //每日各分组图片新增数

//        return json_encode($perdayUsers);
        $statistics = [
            'statistic_newuser_perday' => (int)$perdayUsers,
            'statistic_newpic_perday' => (int)$perdayPictures,
            'statistic_total_user_num' => (int)$users['userNumber'],
            'statistic_total_user_male_num' => (int)$users['maleNumber'],
            'statistic_total_user_female_num' => (int)$users['femaleNumber'],
            'statistic_total_pic_num' => (int)$pictures,
        ];
        $result = $this->statisticsRepository->createStatistics($statistics);
        if($result){
            $this->refreshDayUserNum(); //更新每日新增用户
            $this->refreshSexNumFromDB();//更新用户总数 男女数
            $this->refreshPictureNumFromDB();//更新图片总数
            $this->refreshDayPictureNum();//更新每日新增图片数
        }

        echo '<pre>';
//        记录与更新各分组每日新增图片数
        $categoriesPerdayNum = $this->getCategoriesPerdayNum(); //每日各分组图片新增数
        $intTime = (int)strtotime(date("Y-m-d",intval(time())));//时间戳
        foreach ($categoriesPerdayNum as $key => $value){
            $categoryStatistics = [
                'category_statistics_category_id' => $key,
                'category_statistics_perday_num' => $value,
                'category_statistics_date' => $intTime
            ];
            $this->categoryStatisticsRepository->createCategoryStatistics($categoryStatistics);
        }
        $this->refreshCategoriesPerdayNum();
    }
    /**
     * 重置每日新增用户数
     */
    public function refreshDayUserNum(){
        Redis::set('dayUserNumber',0);
        return json_encode(['code'=>200,'msg'=>'success']);
    }
    /**
     * 获取每日新增用户数
     */
    public function getDayUserNum(){
        $dayUserNumber = 0;
        if(Redis::exists('dayUserNumber')){
            $dayUserNumber = Redis::get('dayUserNumber');
        }else{
            $this->refreshDayUserNum();
            $dayUserNumber = Redis::get('dayUserNumber');
        }
//        return json_encode(['dayUserNumber'=>$dayUserNumber]);
        return $dayUserNumber;
    }
    /**
     * 从数据库里获取并更新redis中男女用户数
     */
    public function refreshSexNumFromDB(){

        $male = $this->analysisRepository->getUserBySex(1);
        if($male){
            //将男性用户数量存储在manNumber中
            $maleNum = count($male);
            Redis::set('maleNumber',$maleNum);
        }
        $female = $this->analysisRepository->getUserBySex(2);
        if($female){
            //将女性用户数量存储在femanNumber中
            $femaleNum = count($female);
            Redis::set('femaleNumber',$femaleNum);
        }
        $userNumber = $this->analysisRepository->getUserNumber();
        if($userNumber){
            //将用户数量存储在userNumber中
            Redis::set('userNumber',$userNumber);
        }
       return json_encode(['code'=>200,'msg'=>'success']);
    }
    /**
     * 从Redis中获取男女用户数
     */
    public function getSexNum(){
        $maleNumber = 0;
        $femaleNumber = 0;
        if(Redis::exists('maleNumber')){
            $maleNumber = Redis::get('maleNumber');
        }else{
            $this->refreshSexNumFromDB();
            $maleNumber = Redis::get('maleNumber');
        }
        if(Redis::exists('femaleNumber')){
            $femaleNumber = Redis::get('femaleNumber');
        }else{
            $this->refreshSexNumFromDB();
            $femaleNumber = Redis::get('femaleNumber');
        }
        if(Redis::exists('userNumber')){
            $userNumber = Redis::get('userNumber');
        }else{
            $this->refreshSexNumFromDB();
            $userNumber = Redis::get('userNumber');
        }
        return ['maleNumber'=>$maleNumber,'femaleNumber'=>$femaleNumber,'userNumber'=>$userNumber];
    }
    /**
     * 从数据库里获取并更新redis中图片总数
     */
    public function refreshPictureNumFromDB(){
        $pictureNumber = $this->analysisRepository->getPictureNumber();
        if($pictureNumber){
            Redis::set('pictureNumber',$pictureNumber);
        }
        return json_encode(['code'=>200,'msg'=>'success']);
    }

    /**
     * 获取图片总数
     */
    public function getPictureNum(){
        $pictureNumber = 0;
        if(Redis::exists('pictureNumber')){
            $pictureNumber = Redis::get('pictureNumber');
        }else{
            $this->refreshPictureNumFromDB();
            $pictureNumber = Redis::get('pictureNumber');
        }
//        return json_encode(['pictureNumber'=>$pictureNumber]);
        return $pictureNumber;
    }
    /**
     * 重置每日新增图片数
     */
    public function refreshDayPictureNum(){
        Redis::set('dayPictureNumber',0);
        return json_encode(['code'=>200,'msg'=>'success']);
    }
    /**
     * 获取每日新增图片数
     */
    public function getDayPictureNum(){
        $dayPictureNumber = 0;
        if(Redis::exists('dayPictureNumber')){
            $dayPictureNumber = Redis::get('dayPictureNumber');
        }else{
            $this->refreshDayPictureNum();
            $dayPictureNumber = Redis::get('dayPictureNumber');
        }
//        return json_encode(['dayPictureNumber'=>$dayPictureNumber]);
        return $dayPictureNumber;
    }
    /**
     * 获取每日各分组的图片数
     */
    public function getCategoriesPerdayNum(){
        $categorys = $this->categoryRepository->getAll();
        $categoriesPerdayNum = [];
        foreach ($categorys as $value){
            if(Redis::exists('dayPictureCategoryId_'.$value->category_id)){
                $categoriesPerdayNum[$value->category_id] = Redis::get('dayPictureCategoryId_'.$value->category_id);
            }else{
                $categoriesPerdayNum[$value->category_id] = 0;
            }
        }
        return $categoriesPerdayNum;
    }
    /**
     * 更新每日各分组的图片数
     */
    public function refreshCategoriesPerdayNum(){
        $categorys = $this->categoryRepository->getAll();
        foreach ($categorys as $value){
            Redis::set('dayPictureCategoryId_'.$value->category_id,0);
        }
    }
    /**
     * 清空所有记录
     */
    public function clearAll(){
        Redis::set('dayPictureNumber',0);//今日新增图片
        Redis::set('dayUserNumber',0);//今日新增用户
        Redis::set('maleNumber',0); //男性用户数
        Redis::set('femaleNumber',0);//女性用户数
        Redis::set('userNumber',0);//用户总数
        Redis::set('pictureNumber',0);//图片总数
        return json_encode(['code'=>200,'msg'=>'success']);
    }
}
